const carousel = require('../db/models/carousel.js');
const mongoose = require('mongoose');
const sd = require('silly-datetime');

// 上传轮播图
exports.upload = async ( { file, body }, res )=> {
  res.status(200).send({ code: 200, message: "上传成功", filePath: `http://localhost:3007/uploads/${file.filename}` });
}

// 获取轮播图列表
exports.list = async ( { query }, res )=> {
  let pageSize = 9999;
  let pageNum = 1;
  if(query.pageSize) pageSize = query.pageSize;
  if(query.pageNum) pageNum = query.pageNum;
  const skip = (pageNum - 1) * pageSize;
  const total = await carousel.countDocuments();
  try{
    const findRes =  await carousel.find({}, { __v: 0 }).skip(skip).limit(pageSize);
    res.status(200).send({ code: 200, message: "查询成功", data: findRes, total });
  }catch(e){
    console.log("err>>>", err);
    res.status(404).send({ code: 404, message: "查询失败" });
  }
}

// 新增轮播图
exports.add = async ( { body }, res )=> {
  if((!body.type && body.type !== 0) || !body.filesPath) {
    res.status(404).send({ code: 404, message: "缺少必要参数" });
    return;
  }
  const params = { 
    type: body.type,
    filesPath: body.filesPath,
    status: body.status || 0,
    describe: body.describe || '暂无',
    createTime: sd.format(new Date(), 'YYYY-MM-DD'),
  };
  const maxRes =  await carousel.aggregate([{$group: { _id: null, maxValue: { $max: '$id' } }}]);
  if(maxRes.length !== 0) await carousel.create({ ...params, id: maxRes[0].maxValue + 1 });
  else await carousel.create({ ...params, id: 0 });
  res.status(200).send({ code: 200, message: "新增成功" });
}

// 搜索轮播图
exports.search = async ( { body }, res )=> {
  const params = {};
  if(body.type || body.type === 0) params.type = body.type
  if(body.status || body.status === 0) params.status = body.status
  try{
    const findRes = await carousel.find( { ...params }, { __v: 0 });
    res.status(200).send({ code: 200, message: "搜索成功", data: findRes, total: findRes.length });
  }catch(err){
    console.log("err>>>", err);
    res.status(404).send({ code: 404, message: "搜索失败" });
  }
}

// 删除轮播图
exports.delete = async ( { query }, res )=> {
  const isObjectId = mongoose.Types.ObjectId.isValid(query.id);
  if(!isObjectId){
    res.status(404).send({ code: 404, message: "缺少必要参数" });
    return;
  }
  const deleteRes = await carousel.deleteOne({ _id: query.id });
  if(deleteRes.deletedCount === 0) {
    res.status(404).send({ code: 404, message: "删除失败" });
    return;
  }
  res.status(200).send({ code: 200, message: "删除成功" });
}

// 获取轮播图详情
exports.detail = async ( { query }, res )=> {
  const isObjectId = mongoose.Types.ObjectId.isValid(query.id);
  if(!isObjectId){
    res.status(404).send({ code: 404, message: "缺少必要参数" });
    return;
  }
  const detailRes = await carousel.find({ _id: query.id });
  if(detailRes.length === 0) {
    res.status(404).send({ code: 404, message: "查询失败" });
    return;
  }
  res.status(200).send({ code: 200, message: "查询成功", data: detailRes[0] });
}

// 编辑轮播图
exports.update = async ( { body }, res )=> {
  const isObjectId = mongoose.Types.ObjectId.isValid(body.id);
  if(!isObjectId || !body.filesPath || (!body.type && body.type !== 0)){
    res.status(404).send({ code: 404, message: "缺少必要参数" });
    return;
  }
  try{
    const findRes = await carousel.find({ _id: body.id });
    if(findRes.length === 0){
      res.status(404).send({ code: 404, message: "修改失败" });
      return;
    }
    let updateOptions = { $set: { filesPath: body.filesPath, type: body.type, describe: body.describe || "", status: body.status || 0 } };
    await carousel.updateOne({ _id: body.id }, updateOptions);
    res.status(200).send({ code: 200, message: "修改成功" });
  }catch(err){
    console.log("err>>>", err);
    res.status(404).send({ code: 404, message: "修改失败" });
  }
}

// 切换轮播图状态
exports.toggle = async ( { body }, res )=> {
  const isObjectId = mongoose.Types.ObjectId.isValid(body.id);
  if(!isObjectId || (!body.status && body.status !== 0)){
    res.status(404).send({ code: 404, message: "缺少必要参数" });
    return;
  }
  try{
    const findRes = await carousel.find({ _id: body.id });
    if(findRes.length === 0){
      res.status(404).send({ code: 404, message: "切换失败" });
      return;
    }
    let updateOptions = { $set: { status: body.status } };
    await carousel.updateOne({ _id: body.id }, updateOptions);
    res.status(200).send({ code: 200, message: "切换成功" });
  }catch(err){
    console.log("err>>>", err);
    res.status(404).send({ code: 404, message: "切换失败" });
  }
}